package whu.cn.whushare.ui.common;

import whu.cn.whushare.R;
import whu.cn.whushare.bean.Result;
import whu.cn.whushare.net.DownloadParams;
import whu.cn.whushare.net.NetResponseListener;
import whu.cn.whushare.ui.main.known.PublishQuestionActivity;
import whu.cn.whushare.ui.main.known.SearchHistoryPop;

import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.SearchView;

import com.android.volley.ParseError;
import com.android.volley.VolleyError;
import com.androidplus.net.NetworkUtil;
import com.androidplus.ui.ToastManager;
import com.androidplus.util.StringUtil;
import com.androidplus.util.ViewMappingUtil;
import com.google.gson.JsonSyntaxException;

/**
 * 文件名：BaseFragment
 * 描 述：当前应用程序UI类中fragment的抽象基类
 * 作 者：胡平平
 */
public abstract class BaseFragment extends Fragment implements NetResponseListener, View.OnClickListener {
	protected TopBar mTopBar;
	private SearchHistoryPop mPop;
	protected int mTotal;
	protected String msg;
	protected int code;

    /**
     * 对顶部菜单条进行初始化
     * @param root - 父控件
     * @param topId - 需要进行初始化的TopBar ID
     * @return view - 初始化操作完成后的view
     */
    protected View initTopBar(View root, int topId) {
		View view = root.findViewById(topId);
		mTopBar = new TopBar();
		ViewMappingUtil.mapView(mTopBar, view);

		mPop = new SearchHistoryPop(getActivity(), mTopBar.searchLayout);

		return view;
	}

    /**
     * 获得顶部菜单条：先初始化TopBar，并为返回键设置事件监听器，然后返回该TopBar
     * @param root - 父控件
     * @return view - 返回当前TopBar
     */
	protected View getTopBar(View root) {
		View view = initTopBar(root, R.layout.common_top_bar);
		mTopBar.goBack.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				goBack();
			}
		});
		return view;
	}

	public void onReShow() {
	}

    //返回上层activity
	public boolean goBack() {
		getActivity().finish();
		return false;
	}

	@Override
    /**
     * 当网络请求成功时调用该方法
     * @param type - URL类型
     * @param response - 返回结果信息
     * @param obj -
     * @see whu.cn.whushare.net.NetResponseListener#onSuccess(whu.cn.whushare.net.DownloadParams.PARSING_TYPE, whu.cn.whushare.bean.Result<T>, java.lang.Object)
     */
	public <T> void onSuccess(DownloadParams.PARSING_TYPE type, Result<T> response, Object obj) {
		msg = response.getMsg();        //返回的错误信息
		code = response.getCode();     //返回的错误码
		mTotal = (int) response.getAllDataCount();      //数据大小
		onSuccess(type, response.getContent(), obj);
	}

	public <T> void onSuccess(DownloadParams.PARSING_TYPE type, T response, Object obj) {

	}

	@Override
    /**
     * 当网络请求失败时调用该方法
     * @param type - URL类型
     * @param response - 返回结果信息
     * @param error -
     * @see whu.cn.whushare.net.NetResponseListener#onFail(whu.cn.whushare.net.DownloadParams.PARSING_TYPE, whu.cn.whushare.bean.Result, com.android.volley.VolleyError)
     */
	public <T> void onFail(DownloadParams.PARSING_TYPE type, Result<T> response, VolleyError error) {
		if (error instanceof ParseError || error.getCause() instanceof JsonSyntaxException) {
			toast(R.string.error_net_data);
		} else if (!NetworkUtil.getInstance(getActivity()).isNetworkOK()) {
			toast(R.string.error_no_network);
		} else if (!TextUtils.isEmpty(response.getMsg())) {
			msg = response.getMsg();
			code = response.getCode();
			toast(response.getMsg());
		}
		onFail(type, error);
	}

	public void onFail(DownloadParams.PARSING_TYPE type, VolleyError error) {

	}

    //自定义消息提示方法
	protected void toast(String str) {
		ToastManager.getInstance(getActivity()).makeToast(str, false, false);
	}
	protected void toast(int id) {
		ToastManager.getInstance(getActivity()).makeToast(getString(id), false, false);
	}

	@Override
    //点击事件响应方法，若被点击的控件为搜索文本框，则跳转到PublishQuestionActivity
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.searchTxt:
			startActivity(PublishQuestionActivity.getStartIntent(getActivity()));
			break;
        default:
            break;
		}
	}
}
